package com.yanqu.road.dao.impl.activity.assistantcostumemall;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.assistantcostumemall.AssistantMallRandomGoods;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author ：zfr
 * @date ：2022/3/23 17:26
 */
public class AssistantCostumeMallDaoImpl extends TempDataObjectDao<AssistantMallRandomGoods> {

    public AssistantMallRandomGoods getGoodsInfo(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_assistant_mall_random_goods where activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(AssistantMallRandomGoods data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_assistant_mall_random_goods(activityId,shopType,goodsInfo,lastUpdateTime"
                + ") VALUES(?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getActivityId());
            call.setInt(i++, data.getShopType());
            call.setString(i++, StringUtils.listToString(data.getMallIdList(), ","));
            call.setLong(i++, data.getLastUpdateTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(AssistantMallRandomGoods data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_assistant_mall_random_goods set goodsInfo=?,lastUpdateTime=?"
                + " where activityId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, StringUtils.listToString(data.getMallIdList(), ","));
            call.setLong(i++, data.getLastUpdateTime());
            call.setInt(i++, data.getActivityId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected AssistantMallRandomGoods parseData(ResultSet rs) throws SQLException {
        AssistantMallRandomGoods data = new AssistantMallRandomGoods();
        data.setActivityId(rs.getInt("activityId"));
        data.setShopType(rs.getInt("shopType"));
        data.setMallIdList(StringUtils.stringToIntegerList(rs.getString("goodsInfo"), ","));
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        return data;
    }
}
